﻿using BLL;
using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class PayOrderForm : Form
    {
        MemberInfo _memberInfo;
        private MemberInfoBLL _memberInfoBLL = new MemberInfoBLL();
        private OrderInfoBLL _orderInfoBLL = new OrderInfoBLL();
        private DeskInfoBLL _deskInfoBLL = new DeskInfoBLL();
        private OrderDTO _orderDTO = new OrderDTO();
        int count, price;
        public PayOrderForm()
        {
            InitializeComponent();
        }


        public void GetData(object obj, EventArgs e)
        {
            _orderDTO = e as OrderDTO;

            label2.Text = _orderDTO.OrderId.ToString();
            label3.Text = _orderDTO.DeskName;
            
            _orderInfoBLL.SelectOrderCountAndPrice(_orderDTO, out count, out price);
            label5.Text = price.ToString();
        }

        public void InitData()
        {
            dgvROrderProduct.DataSource = _orderInfoBLL.SelectProductByOrder(_orderDTO.OrderId);
        }

        private void PayOrderForm_Load(object sender, EventArgs e)
        {
            InitData();
            DisplayContent();

            label13.Text = price.ToString();
            label14.Text = price.ToString();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //如果勾选则显示会员，否则隐藏会员
            if(checkBox1.Checked)
            {
                ShowContent();
                SelectMeberInfo();
            }
            else
            {
                DisplayContent();
                label14.Text = price.ToString();
            }
        }

        private void ShowContent()
        {
            label7.Visible = true;
            textBox1.Visible = true;
            label17.Visible = true;
            label16.Visible = true;
            label9.Visible = true;
            label8.Visible = true;
            label10.Visible = true;
            label11.Visible = true;
        }

        private void DisplayContent()
        {
            label7.Visible = false;
            textBox1.Visible = false;
            label17.Visible = false;
            label16.Visible = false;
            label9.Visible = false;
            label8.Visible = false;
            label10.Visible = false;
            label11.Visible = false;
        }


        private void SelectMeberInfo()
        {
            //根据后4位尾号查询11位会员
            _memberInfo = _memberInfoBLL.SelectMemberByEndnum(textBox1.Text);


            if (_memberInfo != null)
            {
                label16.Text = _memberInfo.Mname;
                label8.Text = _memberInfo.Money.ToString();
                label10.Text = _memberInfo.Discount.ToString();

                label14.Text = (_memberInfo.Discount * price * 0.1).ToString();
            }
            else
            {
                label16.Text = "";
                label8.Text = "";
                label10.Text = "";

                label14.Text = price.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //如果勾选了，并且查询到了会员，那么则使用折扣折扣后的金额结算

            if (checkBox1.Checked)      //选中了会员
            {
                if(_memberInfo != null)
                {
                    MemberCheckOut();
                    this.DialogResult = DialogResult.OK;
                    return;
                }

                //使用非会员结账
                MessageBox.Show("请支付 " + label13.Text + " 元");
                _orderInfoBLL.CheckOut(_orderDTO.OrderId, Convert.ToDouble(label13.Text), 0);
                _deskInfoBLL.UpdateDeskState(_orderDTO.DeskId, 0);
                this.DialogResult = DialogResult.OK;
                return;
            }

            //使用非会员结账
            MessageBox.Show("请支付 " + label13.Text + " 元");
            _orderInfoBLL.CheckOut(_orderDTO.OrderId, Convert.ToDouble(label13.Text), 0);
            _deskInfoBLL.UpdateDeskState(_orderDTO.DeskId, 0);
            this.DialogResult = DialogResult.OK;
            return;
        }

        private void MemberCheckOut()
        {
            if((_memberInfo.Money - Convert.ToDouble(label14.Text)) >=0 )
            {
                //使用会员结账
                //扣减会员表的余额，增加积分
                //设置桌子表为可用状态
                //上传订单结束的时间金额等信息
                _memberInfoBLL.SubMoney(_memberInfo.Mid, Convert.ToDouble(label14.Text));
                _orderInfoBLL.CheckOut(_orderDTO.OrderId, Convert.ToDouble(label14.Text), _memberInfo.Discount);
                _deskInfoBLL.UpdateDeskState(_orderDTO.DeskId, 0);
                MessageBox.Show($"结账成功，开内余额还剩{_memberInfo.Money - Convert.ToDouble(label14.Text)}");

                return;
            }

            MessageBox.Show("余额不足请先进行充值！");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

            SelectMeberInfo();
        }
    }
}
